package com.fjut.kms.dao;

import com.fjut.kms.domain.SoftWares;
import com.fjut.kms.exception.KMSException;
import com.fjut.kms.tools.DBTools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by Empathy on 2017/8/20.
 */
public class SoftWaresJDBCImpl implements SoftWaresDao{
    private static final String SQL_GET_BYCATEGORY="select kmsServer from softwares where category=?";

    public SoftWares getKmsServerByCategory(String category){
        Connection conn= DBTools.getConn();
        PreparedStatement pstmt=null;
        ResultSet rset=null;
        SoftWares sw= new SoftWares();
        try{
            pstmt=conn.prepareStatement(SQL_GET_BYCATEGORY);
            pstmt.setString(1,category);
            rset=pstmt.executeQuery();
            if (rset.next()){
                sw.setKmsServer(rset.getString("kmsServer"));
            }else {
                throw new KMSException("kmsServer记录不存在！");
            }
        }catch (SQLException e){
            e.printStackTrace();
        }finally {
            DBTools.releaseRes(conn,pstmt,null);
        }
        return sw!=null?sw:null;
    }
}
